#!/bin/bash

source /home/shell/util.sh



# 安装函数
function backup () {
    cd /var/lib/jenkins
    mkd /opt/jenkins/backup/
    tar -zcP -f /opt/jenkins/backup/$(date +%Y%m%d_%H_%M_%S).tar.gz config.xml jobs/ users/ secrets/ nodes/
    printInfo "备份完成！"
    printInfo "==================================================="
    echo "`ls -Slh /opt/jenkins/backup/`"
    printInfo "==================================================="
}

function update_backup () {
    rm -rf /opt/jenkins/backup/*
    backup
}

function backup_list () {
    cd /var/lib/jenkins
    mkd /opt/jenkins/backup/
    printInfo "==================================================="
    echo "`ls -Slh /opt/jenkins/backup/`"
    printInfo "==================================================="
}

function translate_backup () {
    backup_list
    read -p "选择传输版本：" backup_version
    sz /opt/jenkins/backup/${backup_version}
    printInfo "传输完成！"
}

function restore () {
    printInfo "恢复版本列表"
    ls -Slh /opt/jenkins/backup/
    read -p "选择恢复版本：" backup_version
    mkd /tmp/jenkins/backup
    rm -rf /tmp/jenkins/backup/*

    printInfo "恢复中，请稍后..."

    tar -xf /opt/jenkins/backup/${backup_version} -C /tmp/jenkins/backup
    
    mv /tmp/jenkins/backup/config.xml /var/lib/jenkins/config.xml
    rm -rf /var/lib/jenkins/jobs/*
    mv /tmp/jenkins/backup/jobs/ /var/lib/jenkins/
    rm -rf /var/lib/jenkins/users/*
    mv /tmp/jenkins/backup/users/ /var/lib/jenkins/
    rm -rf /var/lib/jenkins/nodes/*
    mv /tmp/jenkins/backup/nodes/ /var/lib/jenkins/
    systemctl restart jenkins.service 
    printInfo "恢复完成！"
}

function restoreFromLocal () {
    printInfo "恢复版本列表"
    mkd /opt/jenkins/backup/
    cd /opt/jenkins/backup/
    rz
    ls -Slh /opt/jenkins/backup/

}

function deleteBackup () {
    printInfo "删除版本列表"
    ls -Slh /opt/jenkins/backup/
    read -p "选择删除版本：" backup_version
    rm -rf /opt/jenkins/backup/${backup_version}
}

$1